import common from '@/mixins/common.js';
const tabItems = ['填写基本信息', '填写商户信息', '填写结算账户', '确认提交', '车场协议'];
import BaseInfo from './component/baseInfo';
import Merchant from './component/merchant';
import BankAccount from './component/bankAccount';
import ConfirmInfo from './component/confirmInfo';
import Protocol from './component/protocol';
import { bank_list } from './component/opts';

export default {
  name: 'applyLittleCustom',
  mixins: [common],
  components: {
    BaseInfo,
    Merchant,
    BankAccount,
    ConfirmInfo,
    Protocol
  },
  data() {
    return {
      tabItems,
      currentTab: '填写基本信息',
      formData: {},
      currentCreateType: '企业商户',
      id: null,
      refresh: true
    };
  },
  created() {
    if (this.$route.query.merchantId) {
      this.getParkPayMerchantInfo();
      if (this.$route.query.isPreview) {
        this.currentTab = '确认提交';
      }

      if (this.$route.query.isUpgrade) {
        this.currentCreateType = '企业商户';
      }
      return;
    }
    const wechatPayApplyLocalData = localStorage.getItem('wechatPayApplyLocalData');
    if (wechatPayApplyLocalData) {
      this.formData = JSON.parse(wechatPayApplyLocalData);
    }

    // API.fetchGet('/static/file/wechatPay_bank_data.json').then(res => {
    //     console.log(res)
    //   });
  },
  methods: {
    getParkPayMerchantInfo() {
      API.fetchGet('/valueAdded/getParkPayMerchantInfo', {
        merchantId: this.$route.query.merchantId
      }).then(res => {
        if (!this.$route.query.isUpgrade) {
          this.currentCreateType = res.organizationType ? '企业商户' : '小微商户';
        }

        this.id = res.id;
        this.formatBaseInfo(res);
        this.formatMerchant(res);
        this.formatBankAccount(res);
        if (this.$route.query.isPreview) {
          this.refresh = false;
          this.$nextTick(() => {
            this.refresh = true;
          });
        }
      });
    },
    formatBaseInfo(res) {
      let baseInfo = {
        name: '',
        phone: '',
        email: '',
        storeName: '',
        storeAddressCode: '',
        storeAddress: '',
        storeEntrancePic: '',
        storeEntrancePicBase64: '',
        storeIndoorPic: '',
        storeIndoorPicBase64: '',
        merchantShort: '',
        customTel: '',
        organizationType: '',
        productDesc: '',
        store_province: '',
        store_city: '',
        storeAddressCodeDesc: []
      };
      for (let key in baseInfo) {
        if (res[key]) {
          baseInfo[key] = res[key];
        }
      }
      baseInfo.store_province = res.storeAddressCode ? res.storeAddressCode.split(',')[0] : '';
      baseInfo.store_city = res.storeAddressCode ? res.storeAddressCode.split(',')[1] : '';
      baseInfo.storeAddressCodeDesc = [];

      this.formData.baseInfo = baseInfo;
      this.refresh = false;
      this.$nextTick(() => {
        this.refresh = true;
      });
    },
    formatMerchant(res) {
      let merchant_info = {
        idCardFront: '',
        idCardFrontBase64: '',
        idCardBack: '',
        idCardBackBase64: '',
        certificateHolderName: '',
        idCardNumber: '',
        idCardStartTime: '',
        idCardEndTime: '',
        businessStartTime: '',
        businessEndTime: '',
        businessTime: '',
        idCardValidTime: '',
        registeredAddress: '',
        businessName: '',
        businessLicenseCode: '',
        businessLicenBase64: '',
        businessLicense: '',
        businessScope: '',
        isLongTime: false,
        companyisLongTime: false,
        qualifications: '',
        qualificationsBase64: ''
      };

      for (let key in merchant_info) {
        if (res[key]) {
          merchant_info[key] = res[key];
        }
      }

      if (res.idCardValidTime) {
        const parse_idcard_time = JSON.parse(res.idCardValidTime);
        if (parse_idcard_time[1] === '长期') {
          merchant_info.isLongTime = true;
          merchant_info.idCardStartTime = parse_idcard_time[0];
        } else {
          merchant_info.idCardStartTime = parse_idcard_time[0];
          merchant_info.idCardEndTime = parse_idcard_time[1];
        }
      }

      if (res.businessTime) {
        const parse_business_time = JSON.parse(res.businessTime);
        if (parse_business_time[1] === '长期') {
          merchant_info.companyisLongTime = true;
          merchant_info.businessStartTime = parse_business_time[0];
        } else {
          merchant_info.businessStartTime = parse_business_time[0];
          merchant_info.businessEndTime = parse_business_time[1];
        }
      }
      this.formData.merchantInfo = merchant_info;
    },
    formatBankAccount(res) {
      let bankAccountInfo = {
        bankUserName: '',
        bankNumber: '',
        bankBranchName: '',
        bankType: '',
        bankProvince: '',
        bank_city: '',
        bankCityCode: '',
        bankCity: []
      };

      for (let key in bankAccountInfo) {
        if (res[key]) {
          bankAccountInfo[key] = res[key];
        }
      }
      if (res.bankCityCode) {
        bankAccountInfo.bankProvince = res.bankCityCode.split(',')[0];
        bankAccountInfo.bank_city = res.bankCityCode.split(',')[1];
      }

      if (!bank_list.find(item => item.bank_name === bankAccountInfo.bankType)) {
        bankAccountInfo.otherBankType = bankAccountInfo.bankType;
        bankAccountInfo.bankType = '其他';
      }

      bankAccountInfo.bankCity = [];
      this.formData.bankAccountInfo = bankAccountInfo;
    },
    changeCreateType() {
      this.currentCreateType = this.currentCreateType === '企业商户' ? '小微商户' : '企业商户';
      this.showToast(`已切换为${this.currentCreateType === '小微商户' ? '小微商户' : '企业商户'}申请页面`, 0, 2000);
    },
    setFormData(key, data) {
      this.formData[key] = data;
      localStorage.setItem('wechatPayApplyLocalData', JSON.stringify(this.formData));
    },
    goBackSetp() {
      const stepIndex = this.tabItems.indexOf(this.currentTab);
      this.currentTab = this.tabItems[stepIndex - 1];
    },
    goNextStep() {
      const stepIndex = this.tabItems.indexOf(this.currentTab);
      this.currentTab = this.tabItems[stepIndex + 1];
    },
    goStep(tabName) {
      this.currentTab = tabName;
    }
  },
  computed: {
    showCurrentType() {
      return this.currentCreateType === '企业商户' ? '小微商户' : '企业商户';
    }
  }
};
